/**  
 * Project Name:api-webhook  
 * File Name:ChsiService.java  
 * Package Name:com.example.service.webhook.business.chsi.service  
 * Date:2016年7月26日下午4:07:19  
 * Copyright (c) 2016, yuandong@51dojo.com All Rights Reserved.  
 *  
*/

package com.example.service.webhook.business.alipay.service;

import com.example.service.webhook.business.alipay.api.AlipayClient;
import com.example.service.webhook.business.alipay.billitem.AlipayBillTask;
import com.example.service.webhook.business.alipay.dto.*;
import com.example.service.webhook.business.alipay.entity.*;
import com.example.service.webhook.business.alipay.repository.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName:ChsiService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年7月26日 下午4:07:19 <br/>
 * 
 * @author yuandong
 * @version
 * @since JDK 1.6
 * @see
 */
@Service
public class AlipayService {

	private static final Logger LOGGER = LoggerFactory.getLogger(AlipayService.class);

	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;
	@Autowired
	private AlipayClient alipayClient;
	@Autowired
	private AlipayUserInfoRepository alipayUserInfoRepository;
	@Autowired
	private AlipayTradeRecordRepository alipayTradeRecordRepository;
	@Autowired
	private AlipayWealthRepository alipayWealthRepository;

	@Autowired
	private AlipayBankInfoRepository alipayBankInfoRepository;

	@Autowired
	private AlipayContactInfoRepository alipayContactInfoRepository;

	@Autowired
	private AlipayRecentTraderInfoRepository alipayRecentTraderInfoRepository;

	@Autowired
	private AlipayYuebaoTradeInfoRepository alipayYuebaoTradeInfoRepository;

	@Autowired
	private AlipayTradeCategoryRepository alipayTradeCategoryRepository;

	@Autowired
	private AlipayJieBeiRepository alipayJieBeiRepository;

	@Autowired
	private AlipayDeliverAddressRepository alipayDeliverAddressRepository;
	/**
	 * 
	 * fetchBill:处理支付宝回调 <br/>
	 * 在收到魔蝎的邮箱账单回调时执行<br/>
	 * 学籍信息<br/>
	 * 学历信息<br/>
	 * 
	 * @author yuandong
	 * @param task
	 * @since JDK 1.6
	 */
	public void fetchBill(final AlipayBillTask task) {

		// 这里交给线程池处理，防止下面的业务处理时间太长，导致超时。
		// 超时会导致魔蝎数据进行重试，会收到重复的回调请求

		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
//				saveUserInfo(task);
//				saveTradeInfo(task);
//				saveWealth(task);
				saveData(task);

			}
		}

		);

	}


	private void saveData(AlipayBillTask task){
		try {
			AlipayData alipayData = alipayClient.getData(task.getTaskId());
			if (alipayData != null) {
				if(alipayData.getUserinfo()!=null){
					AlipayUserInfo userinfo = alipayData.getUserinfo();
					AlipayUserInfoEntity alipayUserInfoEntity = new AlipayUserInfoEntity();
					BeanUtils.copyProperties(userinfo, alipayUserInfoEntity);
					alipayUserInfoEntity.setUserId(task.getUserId());
					AlipayUserInfoEntity isExistEntity = alipayUserInfoRepository.getUserInfo(task.getUserId(),
							task.getMappingId());
					if (isExistEntity != null) {
						alipayUserInfoEntity.setCreateTime(isExistEntity.getCreateTime());
						alipayUserInfoEntity.setId(isExistEntity.getId());
						alipayUserInfoRepository.updateUserInfo(alipayUserInfoEntity);
					} else {
						alipayUserInfoRepository.saveUserInfo(alipayUserInfoEntity);
					}
				}

				AlipayWealth alipayWealth = alipayData.getWealth();
				if (alipayWealth != null) {
					AlipayWealthEntity alipayWealthEntity = new AlipayWealthEntity();
					BeanUtils.copyProperties(alipayWealth, alipayWealthEntity);
					alipayWealthEntity.setUserId(task.getUserId());
					AlipayWealthEntity isExistEntity = alipayWealthRepository.getWealth(task.getUserId(),
							task.getMappingId());
					if (isExistEntity != null) {
						alipayWealthEntity.setCreateTime(isExistEntity.getCreateTime());
						alipayWealthEntity.setId(isExistEntity.getId());
						alipayWealthRepository.updateWealth(alipayWealthEntity);
					} else {
						alipayWealthRepository.saveWealth(alipayWealthEntity);
					}
				}


				List<AlipayTradeRecord> tradeRecords = alipayData.getTradeinfo();
				if (tradeRecords != null) {
					for (AlipayTradeRecord alipayTradeRecord : tradeRecords) {
						AlipayTradeRecordEntity alipayTradeRecordEntity = new AlipayTradeRecordEntity();
						BeanUtils.copyProperties(alipayTradeRecord, alipayTradeRecordEntity);
						alipayTradeRecordEntity.setUserId(task.getUserId());
						AlipayTradeRecordEntity isExistEntity = alipayTradeRecordRepository.getTradeRecord(task.getUserId(),
								task.getMappingId(), alipayTradeRecord.getTradeNumber());
						if (isExistEntity != null) {
							// alipayTradeRecordEntity.setCreateTime(isExistEntity.getCreateTime());
							// alipayTradeRecordEntity.setId(isExistEntity.getId());
							// alipayTradeRecordRepository.updateTradeRecord(alipayTradeRecordEntity);
						} else {
							alipayTradeRecordRepository.saveTradeRecord(alipayTradeRecordEntity);
						}
					}


				}

				//保存支付宝绑定银行卡信息
				List<AlipayBankInfo> bankInfos = alipayData.getBankInfos();
				if (null != bankInfos){
					for(AlipayBankInfo bankInfo : bankInfos){
						AlipayBankInfoEntity alipayBankInfoEntity = new AlipayBankInfoEntity();
						BeanUtils.copyProperties(bankInfo,alipayBankInfoEntity);
						alipayBankInfoEntity.setUserId(task.getUserId());
						AlipayBankInfoEntity isExistEntity = alipayBankInfoRepository.getBankInfo(task.getUserId(),task.getMappingId(),bankInfo.getSignId());

						if (null != isExistEntity){
							alipayBankInfoEntity.setCreateTime(isExistEntity.getCreateTime());
							alipayBankInfoEntity.setId(isExistEntity.getId());
							alipayBankInfoRepository.updateBankInfo(alipayBankInfoEntity);
						} else {
							alipayBankInfoRepository.saveBankInfo(alipayBankInfoEntity);
						}

					}
				}

				List<AlipayContactInfo> contactInfos = alipayData.getContactInfos();
				if(null != contactInfos){
					for (AlipayContactInfo alipayContactInfo : contactInfos){
						AlipayContactInfoEntity alipayContactInfoEntity = new AlipayContactInfoEntity();
						BeanUtils.copyProperties(alipayContactInfo,alipayContactInfoEntity);
						alipayContactInfoEntity.setUserId(task.getUserId());
						AlipayContactInfoEntity isExistEntity = alipayContactInfoRepository.getContactInfo(task.getUserId(),task.getMappingId(),alipayContactInfo.getRealName());

						if (null != isExistEntity){
							alipayContactInfoEntity.setCreateTime(isExistEntity.getCreateTime());
							alipayContactInfoEntity.setId(isExistEntity.getId());
							alipayContactInfoRepository.updateContactInfo(alipayContactInfoEntity);
						}
						else {
							alipayContactInfoRepository.saveContactInfo(alipayContactInfoEntity);
						}
					}
				}

				List<AlipayRecentTraderInfo> recentTraderInfos = alipayData.getRecentTraderInfos();
				if (null != recentTraderInfos){
					for (AlipayRecentTraderInfo alipayRecentTraderInfo : recentTraderInfos){
						AlipayRecentTraderInfoEntity alipayRecentTraderInfoEntity = new AlipayRecentTraderInfoEntity();
						BeanUtils.copyProperties(alipayRecentTraderInfo,alipayRecentTraderInfoEntity);
						alipayRecentTraderInfoEntity.setUserId(task.getUserId());
						AlipayRecentTraderInfoEntity isExistEntity = alipayRecentTraderInfoRepository.getRecentTraderInfo(task.getUserId(),task.getMappingId(),alipayRecentTraderInfo.getRealName());

						if (null != isExistEntity){
							alipayRecentTraderInfoEntity.setCreateTime(isExistEntity.getCreateTime());
							alipayRecentTraderInfoEntity.setId(isExistEntity.getId());
							alipayRecentTraderInfoRepository.updateRecentTraderInfo(alipayRecentTraderInfoEntity);
						}else {
							alipayRecentTraderInfoRepository.saveRecentTraderInfo(alipayRecentTraderInfoEntity);
						}
					}
				}

				//保存借呗信息
				AlipayJieBeiInfo alipayJieBeiInfo = alipayData.getAlipayJieBeiInfo();
				if(null != alipayJieBeiInfo){
					AlipayJieBeiEntity alipayJieBeiEntity = new AlipayJieBeiEntity();
					BeanUtils.copyProperties(alipayJieBeiInfo,alipayJieBeiEntity);
					alipayJieBeiEntity.setUserId(task.getUserId());
					AlipayJieBeiEntity isExist = alipayJieBeiRepository.getJieBei(task.getUserId(),alipayJieBeiEntity.getMappingId());
					if(null != isExist){
						alipayJieBeiEntity.setCreateTime(isExist.getCreateTime());
						alipayJieBeiEntity.setId(isExist.getId());
						alipayJieBeiRepository.updateJieBei(alipayJieBeiEntity);
					}else {
						alipayJieBeiRepository.saveJieBei(alipayJieBeiEntity);
					}

				}

				//保存收货地址信息
				List<AlipayDeliverAddressInfo> alipayDeliverAddressInfos = alipayData.getDeliverAddressInfos();
				if(null != alipayDeliverAddressInfos){
					for (AlipayDeliverAddressInfo alipayDeliverAddressInfo : alipayDeliverAddressInfos){
						AlipayDeliverAddressEntity alipayDeliverAddressEntity = new AlipayDeliverAddressEntity();
						BeanUtils.copyProperties(alipayDeliverAddressInfo,alipayDeliverAddressEntity);
						alipayDeliverAddressEntity.setUserId(task.getUserId());
						AlipayDeliverAddressEntity isExist = alipayDeliverAddressRepository.getDeliverAddress(task.getUserId(),alipayDeliverAddressInfo.getMappingId());
						if(null != isExist){
							alipayDeliverAddressEntity.setCreateTime(isExist.getCreateTime());
							alipayDeliverAddressEntity.setId(isExist.getId());
							alipayDeliverAddressRepository.updateDeliverAddress(alipayDeliverAddressEntity);
						}else {
							alipayDeliverAddressRepository.saveDeliverAddress(alipayDeliverAddressEntity);
						}
					}
				}
			}
		} catch (Exception e) {
			LOGGER.error("处理支付宝数据出现异常", e);
		}
	}

//	private void saveUserInfo(AlipayBillTask task) {
//		try {
//			AlipayUserInfo userinfo = alipayClient.getUserInfo(task.getTaskId());
//			if (userinfo != null) {
//				AlipayUserInfoEntity alipayUserInfoEntity = new AlipayUserInfoEntity();
//				BeanUtils.copyProperties(userinfo, alipayUserInfoEntity);
//				alipayUserInfoEntity.setUserId(task.getUserId());
//				AlipayUserInfoEntity isExistEntity = alipayUserInfoRepository.getUserInfo(task.getUserId(),
//						task.getMappingId());
//				if (isExistEntity != null) {
//					alipayUserInfoEntity.setCreateTime(isExistEntity.getCreateTime());
//					alipayUserInfoEntity.setId(isExistEntity.getId());
//					alipayUserInfoRepository.updateUserInfo(alipayUserInfoEntity);
//				} else {
//					alipayUserInfoRepository.saveUserInfo(alipayUserInfoEntity);
//				}
//			}
//		} catch (Exception e) {
//			LOGGER.error("处理支付宝基本信息出现异常", e);
//		}
//
//	}
//
//	private void saveTradeInfo(AlipayBillTask task) {
//		try {
//			List<AlipayTradeRecord> tradeRecords = alipayClient.getTradeInfo(task.getTaskId());
//			if (tradeRecords != null) {
//				for (AlipayTradeRecord alipayTradeRecord : tradeRecords) {
//					AlipayTradeRecordEntity alipayTradeRecordEntity = new AlipayTradeRecordEntity();
//					BeanUtils.copyProperties(alipayTradeRecord, alipayTradeRecordEntity);
//					alipayTradeRecordEntity.setUserId(task.getUserId());
//					AlipayTradeRecordEntity isExistEntity = alipayTradeRecordRepository.getTradeRecord(task.getUserId(),
//							task.getMappingId(), alipayTradeRecord.getTradeNumber());
//					if (isExistEntity != null) {
//						// alipayTradeRecordEntity.setCreateTime(isExistEntity.getCreateTime());
//						// alipayTradeRecordEntity.setId(isExistEntity.getId());
//						// alipayTradeRecordRepository.updateTradeRecord(alipayTradeRecordEntity);
//					} else {
//						alipayTradeRecordRepository.saveTradeRecord(alipayTradeRecordEntity);
//					}
//				}
//
//
//			}
//		} catch (Exception e) {
//			LOGGER.error("处理支付宝交易记录出现异常", e);
//		}
//
//	}
//
//	private void saveWealth(AlipayBillTask task) {
//		try {
//			AlipayWealth alipayWealth = alipayClient.getWealth(task.getTaskId());
//			if (alipayWealth != null) {
//				AlipayWealthEntity alipayWealthEntity = new AlipayWealthEntity();
//				BeanUtils.copyProperties(alipayWealth, alipayWealthEntity);
//				alipayWealthEntity.setUserId(task.getUserId());
//				AlipayWealthEntity isExistEntity = alipayWealthRepository.getWealth(task.getUserId(),
//						task.getMappingId());
//				if (isExistEntity != null) {
//					alipayWealthEntity.setCreateTime(isExistEntity.getCreateTime());
//					alipayWealthEntity.setId(isExistEntity.getId());
//					alipayWealthRepository.updateWealth(alipayWealthEntity);
//				} else {
//					alipayWealthRepository.saveWealth(alipayWealthEntity);
//				}
//			}
//		} catch (Exception e) {
//			LOGGER.error("处理支付宝资产状况出现异常", e);
//		}
//
//	}


}
